package com.yx.cloud.utils;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

/**
 * 动画
 */
public class GuideAnimationtUtils {

    private final static String TAG = "GuideAnimationtUtils";

    private static RotateAnimation rotateAnim;
    private static AlphaAnimation alphaAnimation;


    /**
     * 渐变动画
     *
     * @param view
     */
    public static void startAlphaAnimation(View view) {
        view.setVisibility(View.VISIBLE);
        alphaAnimation = new AlphaAnimation(0.0f, 1.0f);//渐变度从0到1
        alphaAnimation.setDuration(1800);//动画持续时间：2000毫秒
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setRepeatCount(0);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(alphaAnimation);
    }

    /**
     * 停止 渐变动画
     */
    public static void stopAlphaAnimation() {
        if (alphaAnimation != null) {
            alphaAnimation.cancel();
        }
    }


    public static void startTranslateAnimation01(Context context,
                                                 ImageView view1,
                                                 float fromXValue, float toXType, float fromYValue, float toYType, ImageView v1, View v2) {

        AnimationSet animationSet = new AnimationSet(false);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                // X轴的开始位置
                Animation.ABSOLUTE, fromXValue,
                // X轴的结束位置
                Animation.ABSOLUTE, toXType,
                // Y轴的开始位置
                Animation.ABSOLUTE, fromYValue,
                // Y轴的结束位置
                Animation.ABSOLUTE, toYType);

        translateAnimation.setDuration(1500);
        translateAnimation.setRepeatCount(0); // 设置动画重复次数
        translateAnimation.setRepeatMode(Animation.REVERSE);

        animationSet.addAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                v1.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        view1.startAnimation(animationSet);
    }

    public static void startTranslateAnimation02(Context context, ImageView view2, float fromXValue, float toXType, float fromYValue, float toYType,
                                                 int type, ImageView v, View v2) {

        AnimationSet animationSet = new AnimationSet(false);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                // X轴的开始位置
                Animation.ABSOLUTE, fromXValue,
                // X轴的结束位置
                Animation.ABSOLUTE, toXType,
                // Y轴的开始位置
                Animation.ABSOLUTE, fromYValue,
                // Y轴的结束位置
                Animation.ABSOLUTE, toYType);

        translateAnimation.setDuration(1500);
        translateAnimation.setRepeatCount(0); // 设置动画重复次数
        translateAnimation.setRepeatMode(Animation.REVERSE);
        animationSet.addAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.VISIBLE);
                v2.setVisibility(View.VISIBLE);
                startAlphaAnimation(v2);//文字渐变
                if (type == 0) {
                    //第一个界面
                    startTranslateAnimation03(context, v,
                            0, 500, 0, 0);
                } else if (type == 1) {
                    //第二个界面
                    startTranslateAnimation03(context, v,
                            0, -500, 0, 0);
                } else if (type == 2) {
                    startAlphaAnimation(v);
                } else if (type == 3) {
                    startAlphaAnimation(v);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        view2.startAnimation(animationSet);
    }

    /**
     * 平移+旋转动画
     *
     * @param view
     */

    public static void startTranslateAnimation03(Context context, ImageView view, float fromXValue, float toXType, float fromYValue, float toYType) {
//        DisplayMetrics dm = context.getResources().getDisplayMetrics();
//        float width = dm.widthPixels;
//        float x = view.getX();
//        float trx = width - x;
        AnimationSet animationSet = new AnimationSet(false);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                // X轴的开始位置
                Animation.ABSOLUTE, fromXValue,
                // X轴的结束位置
                Animation.ABSOLUTE, toXType,
                // Y轴的开始位置
                Animation.ABSOLUTE, fromYValue,
                // Y轴的结束位置
                Animation.ABSOLUTE, toYType);

        translateAnimation.setDuration(1500);
        translateAnimation.setRepeatCount(1); // 设置动画重复次数
        translateAnimation.setRepeatMode(Animation.REVERSE);
        animationSet.addAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.VISIBLE);
                float scaleX = view.getScaleX();
                if (scaleX > 0) {
                    //设置图片翻转
                    view.setScaleX(-1);
                } else {
                    //设置图片翻转
                    view.setScaleX(1);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                float scaleX = view.getScaleX();
                if (scaleX == -1) {
                    //设置图片翻转
                    view.setScaleX(1);
                } else {
                    view.setScaleX(-1);
                }
            }
        });

        view.startAnimation(animationSet);
    }

    /**
     * 缩放
     *
     * @param v
     */
    @SuppressLint("ObjectAnimatorBinding")
    public static void scaleAlarm2(View v) {
        AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(v, "scaleX", 1, 1.1f, 2);//后几个参数是放大的倍数
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1, 1.1f, 2);
        scaleX.setRepeatCount(3);//永久循环
        scaleY.setRepeatCount(3);
        animatorSetsuofang.setDuration(1000);//时间
        animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
        animatorSetsuofang.start();//开始
    }

    /**
     * 缩放
     *
     * @param v
     */
    @SuppressLint("ObjectAnimatorBinding")
    public static void scaleAlarm(View v) {
        AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(v, "scaleX", 1, 1.1f, 1);//后几个参数是放大的倍数
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1, 1.1f, 1);
        scaleX.setRepeatCount(1);//永久循环
        scaleY.setRepeatCount(1);
        animatorSetsuofang.setDuration(500);//时间
        animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
        animatorSetsuofang.start();//开始
    }
}
